package com.xianbeiying.app.ckgl.downfile;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.xianbeiying.app.ckgl.R;

/**
 * Created by Adminis on 2016/10/14.
 */
public class UpdateAppView  extends Dialog{
    private SparseArray<View> mViews;
    View layout;
    public UpdateAppView(Context context, int themeResId) {
        super(context, themeResId);
        this.mViews=new SparseArray<View>();
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         layout = inflater.inflate(R.layout.dialog_normal_layout, null);
        layout.setTag(this);
       this.setContentView(layout);
        initView();
    }

    private void initView() {
        this.setCancelable(false);
        this.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (dialog == null) {
                        return false;
                    } else {
                        dialog.dismiss();
                    }
                }
                return false;
            }
        });
        this.show();
    }

    public View getConvertView()
    {
        return layout;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId)
    {
        View view = mViews.get(viewId);
        if (view == null)
        {
            view = layout.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }
    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public UpdateAppView setText(int viewId, String text)
    {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }
    /**
     * TextView的事件监听
     * */
    public void setTextOnClickListener(int viewId,View.OnClickListener li){
        getView(viewId).setOnClickListener(li);
    }
}
